package com.boot.controller;


import com.boot.entity.*;
import com.boot.paramer.GetAdsParamer;
import com.boot.service.AdService;
import com.boot.util.AdCommentsResult;
import com.boot.util.AdPushResult;
import com.boot.util.AdResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
@Controller
public class AdController {

	
	@Resource
	private AdService adService;
	
	@RequestMapping("/ad/stickAdFileVideo.do")
	@ResponseBody
	public AdPushResult publishWithVideo(Ad ad,HttpServletRequest request,@RequestParam(value = "files", required = false) CommonsMultipartFile[] files) throws Exception {
		System.out.println("haha:"+ad);
		AdPushResult result = adService.saveWithVideo(ad,request,files);
		return result;
	}
	
	@RequestMapping("/ad/stickAdFile.do")
	@ResponseBody
	public AdPushResult publish(Ad ad,HttpServletRequest request,@RequestParam(value = "files", required = false) CommonsMultipartFile[] files) throws Exception {
		AdPushResult result = adService.save(ad,request,files);
		return result;
	}
	//无文件上传模式
	@RequestMapping("/ad/stickAd.do")
	@ResponseBody
    public AdPushResult publish(Ad ad){
		//System.out.println(ad);
		AdPushResult result=adService.save(ad);
		return result;
    }
	//获取广告列表
	@RequestMapping("/ad/getAds.do")
	@ResponseBody
	public AdResult getAds(GetAdsParamer paramer){//如果采用json方式向上提交数据则应在接受参数前加注解@RequestBody
		System.out.println(paramer);
		AdResult result=null;
		if(paramer.getKeyword()==null || paramer.getKeyword().equals("")){
			result=adService.getAds(paramer);
		}else{
			result=adService.getAdsByKeyword(paramer);
		}
		return result;
	}
	//获取广告列表(ios定位获取广告)
	@RequestMapping("/ad/getAdsios.do")
	@ResponseBody
	public AdResult getAdsIos(GetAdsParamer paramer){//如果采用json方式向上提交数据则应在接受参数前加注解@RequestBody
		System.out.println(paramer);
		AdResult result=adService.getAdsIos(paramer);
		return result;
	}
	
	//获取某条广告内容
	@RequestMapping("/ad/getAdById.do")
	@ResponseBody
	public AdResult getAdById(String id) throws Exception {
		//System.out.println(id);
		AdResult result=adService.getAdById(id);
		return result;
	}
	//记录广告浏览次数
	@RequestMapping("/ad/recordScan.do")
	@ResponseBody
	public AdResult recordScan(Integer clicks,Integer ad_id){
		AdResult result=adService.recordScan(ad_id, ++clicks);
		return result;
	}
	//获取某条广告内容包括user
	@RequestMapping("/ad/getAdAndUserInfo.do")
	@ResponseBody
	public AdResult getAdInfo(String ad_id) throws Exception {
		//System.out.println("ad_id:"+ad_id);
		AdResult result=adService.getAdAndUserInfo(ad_id);
		return result;
	}
	
	//获取某条广告的评论
	@RequestMapping("/ad/getComments.do")
	@ResponseBody
	public AdCommentsResult getAdInfo(String ad_id,String user_id,String store_id,Integer pageIndex,Integer pageSize) throws Exception {
		//System.out.println("ad_id:"+ad_id);
		AdCommentsResult result=adService.getComments(ad_id,user_id,store_id,pageIndex,pageSize);
		return result;
	}
	
	//对广告进行评论
	@RequestMapping("/ad/publishAdComment.do")
	@ResponseBody
	public AdResult publishAdComment(Comment comment){
		//System.out.println(comment.toString());
		AdResult result=adService.publishAdComment(comment);
		return result;
	}
	//删除某条评论的回复
	@RequestMapping("/ad/deleteAdCommentReply.do")
	@ResponseBody
	public AdResult deleteAdCommentReply(String comment_id,String comment_reply_id){
		AdResult result=adService.deleteAdCommentReply(comment_id,comment_reply_id);
		return result;
	}
	
	//发布某条评论的回复
	@RequestMapping("/ad/publishAdCommentReply.do")
	@ResponseBody
	public AdResult publishAdCommentReply(CommentReply commentReply){
		//System.out.println(commentReply);
		String comment_id=commentReply.getComment_id();
		AdResult result=adService.publishAdCommentReply(commentReply);
		return result;
	}
	
	
	//获取某条评论的所有回复
	@RequestMapping("/ad/getCommentReplys.do")
	@ResponseBody
	public AdResult getCommentReplys(String user_id,String comment_id,Integer pageIndex,Integer pageSize){
		AdResult result=adService.getCommentReplys(user_id,comment_id,pageIndex,pageSize);
		return result;
	}
	
	//点赞
	@RequestMapping("/ad/addGood.do")
	@ResponseBody
	public AdResult excuteAddAddgood(String comment_id, String user_id){
		//System.out.println(comment_id+":"+user_id);
		AdResult result=adService.addGood(comment_id,user_id);
		return result;
	}
	//评论的回复点赞
	@RequestMapping("/ad/addReplyGood.do")
	@ResponseBody
	public AdResult addReplyGood(String comment_reply_id, String user_id){
		System.out.println(comment_reply_id+":"+user_id);
		AdResult result=adService.addReplyGood(comment_reply_id,user_id);
		return result;
	}
	
	//删除某条评论
	@RequestMapping("/ad/deleteAdComment.do")
	@ResponseBody
	public AdResult excuteDeleteCommentById(String ad_id,String comment_id){
		AdResult result=adService.deleteCommentById(ad_id,comment_id);
		return result;
	}
	
	//收藏
	@RequestMapping("/ad/collectAd.do")
	@ResponseBody
	public AdResult collectAd(CollectionAd collection){
		AdResult result=adService.collectAdService(collection);
		return result;
	}
	
	/*
	 * android端文件上传测试
	 */
	@RequestMapping("/fileupload.do")
	@ResponseBody
	public AdResult testUploadFile(@RequestParam("file") CommonsMultipartFile[] files){
		//System.out.println(ad.toString());
		AdResult result=new AdResult();
		if(files!=null){
			try {
				for(int i=0;i<files.length;i++){
					InputStream in=files[i].getInputStream();
					File file=new File("E:"+File.separator+""+i+"text.png");
					FileOutputStream fos=new FileOutputStream(file);
					int len=-1;
					byte[] bytes=new byte[1024];
					while((len=in.read(bytes))!=-1){
						fos.write(bytes, 0, len);
					}
					in.close();
					fos.close();
				}
				result.setMsg("成功了");
				result.setStatus(0);
				return result;
			} catch (IOException e) {
				e.printStackTrace();
				result.setMsg("失败了");
				result.setStatus(0);
				return result;
			}
		}
		result.setMsg("失败了");
		result.setStatus(0);
		return result;
	}
	//支付
	@RequestMapping("/ad/doPayByBalance.do")
	@ResponseBody
	public AdResult doPayByBalance(String ad_id,String pay_password,String token){
		//System.out.println(ad_id);
		AdResult adResult=adService.doPayByBalance(ad_id,pay_password,token);
		return adResult;
	}
	
	//添加举报
	@RequestMapping("/ad/addReport.do")
	@ResponseBody
	public AdResult addReport(Report report){
		System.out.println(report);
		AdResult adResult=adService.addReportService(report);
		return adResult;
	}
	
	
}
